﻿using Cysharp.Threading.Tasks;
using UnityEngine;
using System;

/// <summary>
/// 死亡
/// </summary>
public class FSMStateDie : FSMStateBase
{
    protected override string getName => "die";

    public override bool isLoop { get; } = false;

    public override int priority { get; } = UnitDefine.STATE_PRIORITY_NON_INTERRUPTIBLE;

    protected override async void OnEnter()
    {
        var axis = fsm.parameters.GetParam<float>("hurtAxis");
        if (axis == 0)
        {
            isEnd = true;
            return;
        }
        LookAt(axis);
        fsm.entity.physics.velocity = Vector2.zero;
        await UniTask.WaitUntil(() => fsm.entity.animator.IsPlayingClipAndComplete(clipName));
        isEnd = true;
        fsm.entity.Despawn();
    }

    private void LookAt(float axis)
    {
        if (axis != 0)
            fsm.entity.animator.direction = new Vector3(-axis, 1, 1);
    }
}